/*
  @description: 测试老姚的小广告
 * @author: L.Wen
 * @create: 2018-09-29 14:21
 */

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class TestYouliao_2 {
    private static WebDriver driver;
    private WebElement webElement = null;
    private WebElement webElement1 = null;
    private String src;

    static {
        //告诉系统chromedriver.exe的位置，打开Chrome浏览器
        System.setProperty("webdriver.chrome.driver", "chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    /**
     * 打开原始连接
     */
    @Test()
    public void openBD() {
        System.out.println("Test openBD! \n");
        driver.get("https://youliao.163yun.com/h5/list/?appkey=7a5c4732c2084c54bba26859ad5e9541&secretkey=9630363599734af6872167c54a461ee8&s=semi");
    }

    /**
     * 打开1Call标签
     * 自动执行5次
     */
    @Test(priority = 10, invocationCount = 5)
    public void searchCSDN() throws InterruptedException {

        Set<String> windows = driver.getWindowHandles();
        List<String> windowsArr = new ArrayList<>(windows);
        driver.switchTo().window(windowsArr.get(0));
        System.out.println("Test search1Call! \n");
        webElement = driver.findElement(By.xpath("//*[@data-key = \"t_0\"]"));
        webElement.click();
        Thread.sleep(2001);

        // 打开1Call的第一条链接
        System.out.println("Test clickResult! \n");

        webElement = driver.findElement(By.xpath("//*[@target=\"_blank\"]"));
        webElement.click();
        Thread.sleep(2001);

        // 查找广告的超链接
        System.out.println("Test clickAdvertisement! \n");
        Set<String> windows1 = driver.getWindowHandles();
        List<String> windowsArr1 = new ArrayList<>(windows1);
        driver.switchTo().window(windowsArr1.get(1));
        webElement1 = driver.findElement(By.tagName("iframe"));
        src = webElement1.getAttribute("src");
        System.out.println("src: " + src);
        Thread.sleep(2001);

        System.out.println("Test openSrc! \n");
        driver.get(src);
        Thread.sleep(2001);
        webElement = driver.findElement(By.xpath("//*[@target=\"_top\"]"));
        webElement.click();
        Thread.sleep(3001);
        driver.manage().deleteAllCookies();
        driver.close();


    }

}

